<template>
    <div class="devtool-genTemplate-container">
        <el-dialog title="新增生成代码模板" v-model="isShowDialog" width="80%">
            <el-form ref="addGenTemplateFormRef" :model="formData" :rules="formRules" label-width="80px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="模板名称" prop="name">
                            <el-input v-model="formData.name" placeholder="请输入模板名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="命名空间" prop="namespace">
                            <el-select v-model="formData.namespace" placeholder="请选择命名空间" class="w100">
                                <el-option 
                                    v-for="item in namespaces"
                                    :key="item.dictValue"
                                    :label="item.dictName"
                                    :value="item.dictValue">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="生成路径" prop="path">
                            <el-input v-model="formData.path" placeholder="请输入生成路径" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="主题背景" prop="theme">
							<el-select v-model="codemirror.options.theme" placeholder="请选择编辑器主题" clearable class="w100">
                                <el-option label="blackboard" value="blackboard"></el-option>
                                <el-option label="idea" value="idea"></el-option>
                                <el-option label="material" value="material"></el-option>
                                <el-option label="night" value="night"></el-option>
							</el-select>
						</el-form-item>	
					</el-col>
                    <el-col style="min-height: 200px;">
                        <Codemirror ref="codemirrorRef" v-model:value="formData.content" :options="codemirror.options" border/>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import Codemirror from "codemirror-editor-vue3";
import 'codemirror/theme/blackboard.css';
import 'codemirror/theme/idea.css';
import 'codemirror/theme/material.css';
import 'codemirror/theme/night.css';
import "codemirror/mode/groovy/groovy.js";
import { addGenTemplate } from '/@/api/devtool/template/index';
import { getDictionaryByLabel } from '/@/api/system/dictionary/index';

export default {
    name: 'devtool/template/add',
    components: { Codemirror },
    setup(props, context) {
        const addGenTemplateFormRef = ref();
        const codemirrorRef = ref();
        const state = reactive({
            isShowDialog: false,
            namespaces: [],
            formData: {},
            formRules : {
                name: { required: true, message: '模板名称不能为空', trigger: 'blur' },
            },
            codemirror: {
				options: {
					theme: 'idea',
					mode: "groovy",
					line: true,
					lineWrapping: true,
					lineNumbers: true, // 是否在编辑器左侧显示行号
					matchBrackets: true, // 括号匹配
					styleActiveLine: true ,// 光标行高亮
					extraKeys:{"Ctrl-Space":"autocomplete"},
					gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
				}
			}
        });

        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            //加载模板
            getDictionaryByLabel({dictLabel: 'sys_template_namespace'}).then((res: any) => {
                if(res.code == 0){
                    state.namespaces = res.data;
                }
            });
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            addGenTemplateFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addGenTemplateFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        name: state.formData.name,
                        namespace: state.formData.namespace,
                        path: state.formData.path,
                        content: state.formData.content,
                        remark: state.formData.remark,
                    }
                    addGenTemplate(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            addGenTemplateFormRef,
            codemirrorRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>